/**
 * @file cta_main.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_CTA!= 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void cta_main_prepare_destory(lv_obj_t * activity_obj);
static void cta_phonebook_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);

/**********************
 *  STATIC VARIABLES
 **********************/
const struct {
    watchLangTxtId_t txtId;
    lv_event_cb_t event_cb;
} cta_list[] = {
    { WATCH_TEXT_ID_CTA_PHONEBOOK, cta_phonebook_create_btn_event_cb },
};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * cta_main_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_CTA_MAIN;
        activity_ext.create = cta_main_create;
        activity_ext.prepare_destory = cta_main_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);

    lv_obj_t * label_title = lv_label_create(obj, NULL);
    lv_obj_add_style(label_title, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_text_id(label_title, WATCH_TEXT_ID_CTA);
    lv_coord_t label_interval = 10;
    lv_obj_align(label_title, obj, LV_ALIGN_IN_TOP_MID, 0, label_interval);

    lv_obj_t * list = lv_list_create(obj, NULL);
    lv_watch_obj_add_element(list);
    lv_watch_obj_add_element(lv_page_get_scrl(list));
    lv_coord_t interval = 20;
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - lv_obj_get_height(label_title) - interval - 10);
    lv_page_set_edge_flash(list, true);
    lv_obj_add_style(list, LV_PAGE_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_obj_align(list, label_title, LV_ALIGN_OUT_BOTTOM_MID, 0, interval);

    lv_factory_obj_ext_t * ext = factorymode_get_ext();
    if(NULL == ext) return NULL;
    lv_obj_add_style(list, LV_LIST_PART_BG,  &lv_watch_style_transp);

    lv_obj_t * btn;
    lv_obj_t * label;
    uint8_t list_cnt = sizeof(cta_list) / sizeof(cta_list[0]);
    for(uint8_t i = 0; i < list_cnt; i++) {
        btn = lv_list_add_btn(list, NULL, lv_lang_get_text(cta_list[i].txtId));
        lv_watch_obj_add_element(btn);
        lv_obj_add_style(btn, LV_BTN_PART_MAIN, &(ext->list_btn_st));
        lv_obj_set_style_local_border_side(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_BORDER_SIDE_NONE);
        lv_obj_set_style_local_border_side(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_BORDER_SIDE_NONE);
        lv_obj_set_style_local_border_side(btn, LV_BTN_PART_MAIN, LV_STATE_FOCUSED, LV_BORDER_SIDE_NONE);
        lv_obj_set_event_cb(btn, cta_list[i].event_cb);
        label = lv_obj_get_child(btn, NULL);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
        lv_obj_set_width(label, LV_HOR_RES - 10);
    }

    return obj;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void cta_main_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void cta_phonebook_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        cta_phonebook_main_create(NULL);
    }
}

#endif /*USE_LV_WATCH_CTA*/
